package com.cs.controller;

import com.cs.anno.Log;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashSet;

@RestController
@Api(tags = "文件上传的接口")
public class FileController {

    @Autowired
    private FastFileStorageClient fastFileStorageClient;
    @Value("${resources.url}")
    private String HOST;

    /**
     * 文件上传的接口
     * @param file
     * @return
     */
    @PostMapping("/admin/file/upload/element")
    @ApiOperation(value = "文件上传的接口")
    @Log(value = "文件上传")
    public ResponseEntity<String> fileUpload(@RequestParam MultipartFile file){
        String path=null;
        try {
             path= fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), "jpg", new HashSet<>()).getFullPath();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String filePath=HOST+"/"+path;
        return ResponseEntity.ok(filePath);
    }
}
